Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V6/Nu7 implementation #73

Merged
merged 85 commits into from
Oct 10, 2024
Merged

V6/Nu7 implementation #73

merged 85 commits into from
Oct 10, 2024

Conversation

alexeykoren
Copy link
Collaborator

ZSA is implemented across all the codebase, including:

TransactionData now contains 2 extra components - OrchardZsaBundle and IssueBundle

Serialization/deserialization of the new components

Digests of the new components are taken into account during calculation of sighash/txid.
TODO: we are waiting for test vectors and small ordering fixes to be merged to be able to check txid calculation

Nu7 network upgrade is introduced, although currently "nu6" zcash_unstable flag is used to separate ZSA from regular build. All nu7-related "nu6" flags are annotated with /* TODO nu7 */ comment

Builder methods for ZSAs

Wallet-related functionality (zcash_client_backend, zcash_client_sqlite) are disabled since they do not support ZSAs

alexeykoren and others added 11 commits July 30, 2024 15:20
This PR updates the `zcash_primitives` crate to align with the recent
changes in the `sapling-crypto` crate, which was updated to sync with
the latest changes in `zcash_note_encryption`.

### Changes:
- Updated the usage of `enc_ciphertext` to call `.as_ref()` to match the
new return type.
- Adjusted the import of the `ENC_CIPHERTEXT_SIZE` constant to use the
definition from `sapling-crypto` instead of `zcash_note_encryption`, as
the constant has been moved to `sapling-crypto`.

Co-authored-by: Dmitry Demin <[email protected]>
This updates the test vectors to be of version 6 (as opposed to
version 7).

It also provides a new file for the V6 test vectors, an improvement to
having them in the same file but behind feature flags.

It provides a error for when the asset description string is not a UTF8
encoding.
@alexeykoren alexeykoren changed the title Txv6 separate bundles rebased V6/Nu7 implementation Sep 22, 2024
@alexeykoren alexeykoren mentioned this pull request Sep 22, 2024
Copy link

@PaulLaux PaulLaux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added some comments

.github/workflows/ci.yml Show resolved Hide resolved
let tx = Transaction::read(&tv.tx[..], BranchId::Nu5).unwrap();
let tx = Transaction::read(
&tv.tx[..],
#[cfg(not(zcash_unstable = "nu6"))] /* TODO nu7 */ BranchId::Nu5,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#[cfg(not(zcash_unstable = "nu6")) /* TODO nu7 */ ] + new line

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -345,7 +403,13 @@ impl<A: Authorization> TransactionData<A> {
transparent_bundle: Option<transparent::Bundle<A::TransparentAuth>>,
sprout_bundle: Option<sprout::Bundle>,
sapling_bundle: Option<sapling::Bundle<A::SaplingAuth, Amount>>,
orchard_bundle: Option<orchard::Bundle<A::OrchardAuth, Amount>>,
orchard_bundle: Option<orchard::Bundle<A::OrchardAuth, Amount, OrchardVanilla>>,
#[cfg(zcash_unstable = "nu6" /* TODO nu7 */ )] orchard_zsa_bundle: Option<

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new line

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

#[cfg(zcash_unstable = "nu6" /* TODO nu7 */ )] orchard_zsa_bundle: Option<
orchard::Bundle<A::OrchardZsaAuth, Amount, OrchardZSA>,
>,
#[cfg(zcash_unstable = "nu6" /* TODO nu7 */ )] issue_bundle: Option<

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new line

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

) -> Option<
orchard::bundle::Bundle<B::OrchardAuth, Amount, OrchardVanilla>,
>,
#[cfg(zcash_unstable = "nu6" /* TODO nu7 */ )] f_zsa_orchard: impl FnOnce(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new line

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

version in Just(version)
) -> TransactionData<Authorized> {
TransactionData {
TransactionData::<Authorized> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is needed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

Comment on lines 20 to 26
use byteorder::WriteBytesExt;

#[cfg(zcash_unstable = "zfuture")]
use zcash_encoding::{CompactSize, Vector};

#[cfg(zcash_unstable = "zfuture")]
use crate::transaction::{components::tze, TzeDigests};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change not required

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed, reverted

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed, reverted

) -> Blake2bHash {
// Currently to_hash is designed in a way that it supports both v5 and v6 signature hash
v5_v6_signature_hash(tx, signable_input, txid_parts)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this file is not connected in mod.rs and function is unused.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure we need it

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

}

#[cfg(zcash_unstable = "nu6" /* TODO nu7 */ )]
fn digest_orchard_zsa(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comment wrt digest_orchard

@@ -516,6 +578,7 @@ impl TransactionDigest<Authorized> for BlockTxCommitmentDigester {
transparent_digest: Self::TransparentDigest,
sapling_digest: Self::SaplingDigest,
orchard_digest: Self::OrchardDigest,
#[cfg(zcash_unstable = "nu6" /* TODO nu7 */ )] issue_digest: Self::IssueDigest,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new line

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}

#[cfg(zcash_unstable = "nu6" /* TODO nu7 */ )]
fn digest_orchard_zsa(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comment wrt digest_orchard

Copy link

@PaulLaux PaulLaux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good overall, added final fixes.

components/zcash_protocol/src/consensus.rs Show resolved Hide resolved
let asset_descr: String = String::from_utf8(asset_descr_bytes)
.map_err(|_| Error::new(ErrorKind::InvalidData, "Asset Description not valid UTF-8"))?;
let notes = Vector::read(&mut reader, |r| read_note(r))?;
let finalize = reader.read_u8()? != 0;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If Muhammad won't go to the mountain, then the mountain must come to Muhammad.

    let finalize = match reader.read_u8()? {
        0 => false,
        1 => true,
        _ => return Err(Error::new(ErrorKind::InvalidData, "Invalid value for finalize")),
    };

Ok(())
}

fn write_action<W: Write>(action: &IssueAction, mut writer: W) -> io::Result<()> {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider changing the parameters order to be compatible with existing code (ie CompactSize::write())
also it will be more compatible with the order in Vector::write(&mut writer, action.notes(), |w, note| write_note(note, w))?; and will allow to omit the parameters completely in some places (same for write_note())

Ok(())
}

pub fn write_note<W: Write>(note: &Note, writer: &mut W) -> io::Result<()> {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replacing the order will allow:

fn write_action<W: Write>(action: &IssueAction, mut writer: W) -> io::Result<()> {
    ...
    Vector::write(&mut writer, action.notes(),  write_note)?;
    ...
    Ok(())
}

pub fn write_note<W: Write>( writer: &mut W,note: &Note) -> io::Result<()> {
...
}

}
}

/// Writes an [`orchard::Bundle`] in the v6 transaction format.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/// Writes an [orchard::Bundle] in the appropriate transaction format

@@ -967,6 +1189,7 @@ pub trait TransactionDigest<A: Authorization> {
transparent_digest: Self::TransparentDigest,
sapling_digest: Self::SaplingDigest,
orchard_digest: Self::OrchardDigest,
#[cfg(zcash_unstable = "nu6" /* TODO nu7 */ )] issue_digest: Self::IssueDigest,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new line plz

zcash_primitives/src/transaction/mod.rs Show resolved Hide resolved
@@ -99,7 +111,16 @@ pub enum Error<FE> {
SaplingBuilderNotAvailable,
/// The builder was constructed with a target height before NU5 activation, but an Orchard
/// spend or output was added.
OrchardBuilderNotAvailable,
OrchardBundleNotAvailable,
/// The issuance bundle not initialized.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove one space after ///

.init_issuance_bundle::<FeeError>(iak, asset.clone(), None)
.unwrap();

let binding = builder.mock_build_no_fee(OsRng).unwrap().into_transaction();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let tx = might be a better name

Comment on lines +386 to +388
orchard_bundle: Option<orchard::bundle::Bundle<A::OrchardAuth, Amount, OrchardVanilla>>,
#[cfg(zcash_unstable = "nu6" /* TODO nu7 */ )]
orchard_zsa_bundle: Option<orchard::bundle::Bundle<A::OrchardZsaAuth, Amount, OrchardZSA>>,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For safety we should make them mutually exclusive using Enum as discussed.

@@ -113,7 +122,7 @@ pub fn write_v6_bundle<W: Write>(
) -> io::Result<()> {
if let Some(bundle) = bundle {
Vector::write_nonempty(&mut writer, bundle.actions(), |w, action| {
write_action(action, w)
write_action(w, action)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vector::write_nonempty(&mut writer, bundle.actions(), write_action)?;

@PaulLaux PaulLaux merged commit 7301f78 into zsa1 Oct 10, 2024
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants